package bluetoothcomm;

import gnu.io.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import monitor.StreamEvent;
import monitor.StreamEventListener;


public class SerialReader implements SerialPortEventListener{

    static InputStream			in;
    static boolean isCapturingImage = false;
    int len = 0;
    byte[] buffer = new byte[1024];

    private List<StreamEventListener> streamListeners = new ArrayList<StreamEventListener>();
    private List<ObstructionEventListener> obstructionListeners = new ArrayList<ObstructionEventListener>();

    public SerialReader() {
    	super();
    }

    public SerialReader(InputStream in)
    {
    	super();
    	SerialReader.in = in;
    }

    public void addStreamListener(StreamEventListener listener) {
    	streamListeners.add(listener);
    }

    public void removeStreamListener(StreamEventListener listener) {
    	streamListeners.remove(listener);
    }

    protected void notifyListeners(StreamEvent e) {
    	for (StreamEventListener listener : streamListeners) {
    		listener.streamAvailable(e);
    	}
    }

    public void addObstructionListener(ObstructionEventListener listener) {
    	obstructionListeners.add(listener);
    }

    public void removeObstructionListener(ObstructionEventListener listener) {
    	obstructionListeners.remove(listener);
    }

    protected void notifyObstructionListeners(ObstructionEvent e) {
    	for (ObstructionEventListener listener : obstructionListeners) {
    		listener.ObstructionAvailable(e);
    	}
    }

	public void connect() {

	}

	public void close() {

	}


//	private int beginCounter=30 ;

	@Override
	public void serialEvent(SerialPortEvent arg0) {
		// TODO Auto-generated method stub
		//int len = 0;
    	//   byte[] buffer = new byte[1024];
    	   int data;
    	   try
	       {
	          //  int len = 0;
	            while ( ( data = in.read()) > -1 )
	            //while ((data=in.read()) != '\n')
	            {

	//            	if(data=='s')
	//               { len = 0;
	////            	System.out.println("Stream event string "+(char)data);
	//            	break;
	//               }
	            	if ( data == '\n' ) {
	//                	System.out.println("Stream event string"+data);
	            		if (buffer[len-1] == '\r')
	            			len--;
	                	String str = new String(buffer,0,len);
	                	notifyListeners(new StreamEvent(str));
	                	len = 0;
	                	buffer = new byte[1024];
	                    break;
	                }/* else if (data> 200 && data <= 255)
	                {
	                	//System.out.println("Stream event string "+ data);
	                	String str2 = (255-data)+"";
		            	notifyObstructionListeners(new ObstructionEvent(str2));
	                }*/else
	                {
	                	buffer[len++] = (byte) data;
	                }

	            }


	//            System.out.println("Stream event string "+str);
	        //start call the listener after begincounter times.
	        // skip  the data at the beginning
	//            if(beginCounter>0)
	//            {beginCounter--;}
	//            else
/*	            if(len==3){
	            	 String str = new String(buffer,1,1);
	            	notifyObstructionListeners(new ObstructionEvent(str));


	            }else
	            {*/
	            	// str = new String(buffer,0,len);
	            	//notifyListeners(new StreamEvent(str));

//	            }
	        }

	        catch ( IOException e )
	        {
	            e.printStackTrace();
	        }
       }




	}


